package cn.flying.cloud.core.event;// RequestCompletedEventListener.java

import javax.annotation.Resource;

import java.time.Duration;
import java.time.ZoneId;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import cn.flying.cloud.system.common.service.UserService;
import cn.flying.cloud.system.log.entity.SysOperLog;

/**
 * 访问日志记录事件监听器
 *
 * @author: admin
 * @date: 2024年07月16日 17:16
 * @version: 1.0
 */
@Component
public class RequestCompletedEventListener {

    @Resource
    private UserService userService;

    @EventListener
    public void handleRequestCompletedEvent(RequestLogEvent event) {
        SysOperLog log = new SysOperLog();
        log.setUserId(event.getUserId());
        log.setUserName(event.getUsername());
        log.setOperIp(event.getIp());
        log.setOperTime(event.getStartTime());
        log.setOperUri(event.getUri());
        log.setOperType(event.getType());
//        log.setContent("");
//        log.setParams("");
        log.setDuration(Duration.between(event.getStartTime().atZone(ZoneId.systemDefault()).toInstant(), event.getEndTime().atZone(ZoneId.systemDefault()).toInstant()).toMillis());
        log.setTenantId(event.getTenantId());
        userService.insertOperLog(log);
    }
}